package com.md.api.common.utils;

import java.util.Random;

public class CodeUtil {
    private static final char[] number = new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9'};
    private static final char[] r = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    private static final char b = 'a';
    private static final int binLen;
    private static final int s = 6;
    private static final String e = "atgsghj";

    public CodeUtil() {
    }

    public static String toSerialCode(long id) {
        char[] buf = new char[32];

        int charPos;
        for(charPos = 32; id / (long)binLen > 0L; id /= (long)binLen) {
            int ind = (int)(id % (long)binLen);
            --charPos;
            buf[charPos] = r[ind];
        }

        --charPos;
        buf[charPos] = r[(int)(id % (long)binLen)];
        String str = new String(buf, charPos, 32 - charPos);
        if (str.length() < 6) {
            StringBuilder sb = new StringBuilder();
            sb.append('a');
            Random rnd = new Random();

            for(int i = 1; i < 6 - str.length(); ++i) {
                sb.append(r[rnd.nextInt(binLen)]);
            }

            str = str + sb.toString();
        }

        return str;
    }

    public static String toSerialCode2(long id) {
        char[] buf = new char[32];

        int charPos;
        for(charPos = 32; id / (long)binLen > 0L; id /= (long)binLen) {
            int ind = (int)(id % (long)binLen);
            --charPos;
            buf[charPos] = r[ind];
        }

        --charPos;
        buf[charPos] = r[(int)(id % (long)binLen)];
        String str = new String(buf, charPos, 32 - charPos);
        if (str.length() < 6) {
            StringBuilder sb = new StringBuilder();
            sb.append("atgsghj".subSequence(0, 6 - str.length()));
            str = str + sb.toString();
        }

        return str;
    }

    public static long codeToId(String code) {
        char[] chs = code.toCharArray();
        long res = 0L;

        for(int i = 0; i < chs.length; ++i) {
            int ind = 0;

            for(int j = 0; j < binLen; ++j) {
                if (chs[i] == r[j]) {
                    ind = j;
                    break;
                }
            }

            if (chs[i] == 'a') {
                break;
            }

            if (i > 0) {
                res = res * (long)binLen + (long)ind;
            } else {
                res = (long)ind;
            }
        }

        return res;
    }

    public static String verifyCode(int length) {
        char[] buf = new char[length];

        for(int i = 0; i < length; ++i) {
            Random rnd = new Random();
            int index = rnd.nextInt(number.length);
            buf[i] = number[index];
        }

        String str = new String(buf);
        return str;
    }

    static {
        binLen = r.length;
    }
}
